package com.uduemc.biso.master.backend.service.impl;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.github.pagehelper.PageInfo;
import com.uduemc.biso.core.common.dto.CBMastermanageAppend;
import com.uduemc.biso.core.common.extities.CMasterUserData;
import com.uduemc.biso.core.common.feign.CMasterUserFeign;
import com.uduemc.biso.core.extities.center.MasterUser;
import com.uduemc.biso.core.feign.MasterUserFeign;
import com.uduemc.biso.core.utils.RestResult;
import com.uduemc.biso.core.utils.ResultUtil;
import com.uduemc.biso.master.backend.request.MastermanageAppend;
import com.uduemc.biso.master.backend.service.MastermanageService;

@Service
public class MastermanageServiceImpl implements MastermanageService {

	@Autowired
	private MasterUserFeign masterUserFeign;

	@Autowired
	private CMasterUserFeign cMasterUserFeign;

	@Autowired
	private PasswordEncoder passwordEncoder;

	@Override
	public MasterUser getById(long id)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = masterUserFeign.findOne(id);
		MasterUser data = ResultUtil.data(restResult, MasterUser.class);
		return data;
	}

	@Override
	public MasterUser getByUsername(String username)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = masterUserFeign.findByUsername(username);
		MasterUser data = ResultUtil.data(restResult, MasterUser.class);
		return data;
	}

	@Override
	public MasterUser userAppend(MastermanageAppend mastermanageAppend)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		String passwordHash = passwordEncoder.encode(mastermanageAppend.getPasswordHash());
		MasterUser makeMasterUser = mastermanageAppend.makeMasterUser();
		makeMasterUser.setPasswordHash(passwordHash).setNumberKey(null).setStatus((short) 1);
		CBMastermanageAppend cBMastermanageAppend = new CBMastermanageAppend();
		cBMastermanageAppend.setMasterUser(makeMasterUser);
		RestResult userAppend = cMasterUserFeign.userAppend(cBMastermanageAppend);
		MasterUser data = ResultUtil.data(userAppend, MasterUser.class);
		return data;
	}

	@Override
	public PageInfo<CMasterUserData> userInfos(String username, short status, short loginNode, int pageNumber,
			String orderByString)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {

		RestResult restResult = cMasterUserFeign.userInfo(username, "", status, loginNode, pageNumber, 12,
				orderByString);
		@SuppressWarnings("unchecked")
		PageInfo<CMasterUserData> data = (PageInfo<CMasterUserData>) ResultUtil.data(restResult,
				new TypeReference<PageInfo<CMasterUserData>>() {
				});
		return data;
	}

	@Override
	public MasterUser updateById(MasterUser masterUser)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = masterUserFeign.updateById(masterUser);
		MasterUser data = ResultUtil.data(restResult, MasterUser.class);
		return data;
	}

}
